//
//  GJFillUPMapBottomView.m
//  xmgjAppFront
//
//  Created by Zeki on 2020/7/10.
//  Copyright © 2020 guojutech. All rights reserved.
//

#import "GJFillUPMapBottomView.h"
#import <AMapFoundationKit/AMapFoundationKit.h>
@interface GJFillUPMapBottomView ()
@property (nonatomic, strong)UILabel *nameLabel;
@property (nonatomic, strong)UILabel *addressLabel;
@property (nonatomic, strong)UILabel *distanceLabel;
@property (nonatomic, strong)UIButton *guideButton;
@property (nonatomic, strong)UIImageView *iconImageView;
@property (nonatomic, strong)UIImageView *locationImageView;

@property (nonatomic, strong)UIImageView *noneDataImageView;
@property (nonatomic, strong)UILabel *noneDataLabel;
@end

@implementation GJFillUPMapBottomView

- (instancetype)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) {
    self.layer.cornerRadius = 8;
    self.layer.masksToBounds = YES;
    self.backgroundColor = [UIColor whiteColor];
    [self initlizationSubViews];
  }
  return self;
}

- (void)initlizationSubViews{
  [self addSubview:self.nameLabel];
  [self addSubview:self.addressLabel];
  [self addSubview:self.distanceLabel];
  [self addSubview:self.guideButton];
  [self addSubview:self.iconImageView];
  [self addSubview:self.locationImageView];
  
  
  [self addSubview:self.noneDataLabel];
  [self addSubview:self.noneDataImageView];
  
  [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.left.mas_equalTo(12);
    make.width.height.mas_equalTo(56);
  }];
  [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.iconImageView.mas_right).offset(10);
    make.top.equalTo(self.iconImageView);
    make.right.equalTo(self.mas_right).offset(-12);
    make.height.mas_equalTo(22);
  }];
  [self.distanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.right.equalTo(self.nameLabel);
    make.height.mas_equalTo(16);
    make.bottom.equalTo(self.iconImageView);
  }];
 
  [self.locationImageView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.iconImageView);
    make.width.height.mas_equalTo(12);
    make.top.equalTo(self.iconImageView.mas_bottom).offset(12);
  }];
  [self.addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.equalTo(self.locationImageView);
    make.height.mas_equalTo(16);
    make.left.equalTo(self.locationImageView.mas_right).offset(4);
    make.right.equalTo(self.nameLabel);
  }];
  
  [self.guideButton mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.mas_equalTo(21);
    make.right.mas_equalTo(-21);
    make.height.mas_equalTo(40);
    make.bottom.mas_equalTo(-18);
  }];
  
  [self.noneDataImageView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.mas_equalTo(27);
    make.width.mas_equalTo(73);
    make.height.mas_equalTo(64.5);
    make.centerX.equalTo(self);
  }];
  
  [self.noneDataLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.noneDataImageView.mas_bottom).offset(16);
    make.centerX.width.equalTo(self);
    make.height.mas_equalTo(40);
  }];
 
  [self showNoneDataView:YES];
}
- (void)buttonClickAction:(UIButton *)sender{
  
  [GJUtil openURLMapNavgationLocation:AMapCoordinateConvert(CLLocationCoordinate2DMake([self.model.latitude floatValue], [self.model.longitude floatValue]), AMapCoordinateTypeBaidu)];
}
- (void)showNoneDataView:(BOOL)isShow{
  if (isShow) {
    self.addressLabel.hidden = YES;
    self.distanceLabel.hidden = YES;
    self.nameLabel.hidden = YES;
    self.iconImageView.hidden = YES;
    self.locationImageView.hidden = YES;
    self.guideButton.hidden = YES;
    self.noneDataImageView.hidden = NO;
    self.noneDataLabel.hidden = NO;
  }else{
    self.addressLabel.hidden = NO;
    self.distanceLabel.hidden = NO;
    self.nameLabel.hidden = NO;
    self.iconImageView.hidden = NO;
    self.locationImageView.hidden = NO;
    self.guideButton.hidden = NO;
    self.noneDataImageView.hidden = YES;
    self.noneDataLabel.hidden = YES;
  }
}
#pragma mark -- getter
- (UILabel *)nameLabel{
  if (!_nameLabel) {
    _nameLabel = [UILabel new];
    _nameLabel.textAlignment = NSTextAlignmentLeft;
    _nameLabel.font = kSystemFontOfSize(16);
    _nameLabel.textColor = UIColorHex(#333232);
    _nameLabel.adjustsFontSizeToFitWidth = YES;
  }
  return _nameLabel;
}
- (UILabel *)distanceLabel{
  if (!_distanceLabel) {
    _distanceLabel = [UILabel new];
    _distanceLabel.textAlignment = NSTextAlignmentLeft;
    _distanceLabel.textColor = HexColor(#999796);
    _distanceLabel.font = kSystemFontOfSize(12);
    _distanceLabel.adjustsFontSizeToFitWidth = YES;
  }
  return _distanceLabel;
}

- (UILabel *)addressLabel{
  if (!_addressLabel) {
    _addressLabel = [UILabel new];
    _addressLabel.font = kSystemFontOfSize(12);
    _addressLabel.textAlignment = NSTextAlignmentLeft;
    _addressLabel.textColor = HexColor(#666564);
    _addressLabel.adjustsFontSizeToFitWidth = YES;
  }
  return _addressLabel;;
}
- (UIButton *)guideButton{
  if (!_guideButton) {
    _guideButton = [UIButton new];
    [_guideButton setTitle:@"导航去加油" forState:UIControlStateNormal];
    [_guideButton setImage:[UIImage imageNamed:@"mapModule_distance"] forState:UIControlStateNormal];
    _guideButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    // left the image
    _guideButton.imageEdgeInsets = UIEdgeInsetsMake(0.0, -10, 0.0, 0.0);
    // right the text
    _guideButton.titleEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, -10);
    _guideButton.backgroundColor = HexColor(#BA914A);
    _guideButton.layer.cornerRadius = 8;
    [_guideButton addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
  }
  return _guideButton;
}
- (UIImageView *)iconImageView{
  if (!_iconImageView) {
    _iconImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mapModule_Petrolicon"]];
  }
  return _iconImageView;
}
-(UIImageView *)locationImageView{
  if (!_locationImageView) {
    _locationImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mapModule_position"]];
  }
  return _locationImageView;
}


- (UILabel *)noneDataLabel{
  if (!_noneDataLabel) {
    _noneDataLabel  = [UILabel new];
    _noneDataLabel.textAlignment = NSTextAlignmentCenter;
    _noneDataLabel.font =kSystemFontOfSize(14);
    _noneDataLabel.textColor = HexColor(#9B9A97);
    _noneDataLabel.numberOfLines = 2;
    _noneDataLabel.text = @"对不起，您附近没有符合的加油站，\n换个地方再试试吧!";
  }
  return _noneDataLabel;
}

- (UIImageView *)noneDataImageView{
  if (!_noneDataImageView) {
    _noneDataImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mapModule_noneData"]];
  }
  return _noneDataImageView;
}
#pragma mark -- setter
- (void)setModel:(GJPetrolStationModel *)model{
  _model = model;
  self.addressLabel.text = [_model.address gj_hasValue] ? _model.address : @"";
  CGFloat distance = [_model.distanceUm floatValue];
  self.distanceLabel.text = [NSString stringWithFormat:@"距您：%.2fkm",(distance / 1000)];
  self.nameLabel.text = [_model.stationName gj_hasValue]? _model.stationName : @"";
  [self showNoneDataView:NO];
}
- (CGFloat)viewHeight{
  return 173;
}
@end
